package com.hj.dapp.business.mainmaterial.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hj.dapp.business.mainmaterial.mapper.MainMaterialMapper;
import com.hj.dapp.business.mainmaterial.model.MainMaterial;
import com.hj.dapp.business.mainmaterial.service.MainMaterialService;
import com.hj.dapp.common.request.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @author 胡奥程
 * @version 1.0
 * @date 2024/1/27
 */
@Service
public class MainMaterialServiceImpl implements MainMaterialService {
    @Autowired
    private MainMaterialMapper mainMaterialMapper;

    @Override
    public MainMaterial get(Long id) {
        return mainMaterialMapper.selectById(id);
    }

    @Override
    public void save(MainMaterial mainMaterial) {
        mainMaterialMapper.insert(mainMaterial);
    }

    @Override
    public IPage<MainMaterial> page(PageRequest<MainMaterial> pageRequest) {
        Page<MainMaterial> page = new Page<>();
        page.setCurrent(pageRequest.getCurrent());
        page.setSize(pageRequest.getSize());
        QueryWrapper<MainMaterial> wrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(pageRequest.getQuery())) {
            if (!ObjectUtils.isEmpty(pageRequest.getQuery().getName())) {
                wrapper.like("name", pageRequest.getQuery().getName());
            }
            if (!ObjectUtils.isEmpty(pageRequest.getQuery().getBrand())) {
                wrapper.eq("brand", pageRequest.getQuery().getBrand());
            }
            if (!ObjectUtils.isEmpty(pageRequest.getQuery().getCategory())) {
                wrapper.eq("category", pageRequest.getQuery().getCategory());
            }
        }
        return mainMaterialMapper.selectPage(page,wrapper);
    }

    @Override
    public void delete(Long id) {
        mainMaterialMapper.deleteById(id);
    }

    @Override
    public void edit(MainMaterial mainMaterial) {
        mainMaterialMapper.updateById(mainMaterial);
    }

    @Override
    public List<MainMaterial> list(Long categoryId) {
        QueryWrapper<MainMaterial> wrapper = new QueryWrapper<>();
        wrapper.eq("category", categoryId);
        return mainMaterialMapper.selectList(wrapper);
    }

}
